//
//  PainterUserAVGView.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/25.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterUserAVGView.h"

using namespace ytx;

@interface PainterUserAVGView()

@property (nonatomic, assign) NSNumber * economicCalendarDataSeletedIndex;

@end

@implementation PainterUserAVGView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.economicCalendarDataSeletedIndex = @(0);
    }
    return self;
}

-(BOOL) hasData
{
    return [super hasData] && !(self.economicCalendarDataSeletedIndex == nil);
}

- (void) resetEconomicCalendarDataSeletedIndex
{
    self.economicCalendarDataSeletedIndex = @(0);
}

- (void) paint:(ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter
{
    [self drawEconomicCalendarBanner:viewModel withPainter:painter withEconomicCalendarDataSeletedIndex:self.economicCalendarDataSeletedIndex];
}

- (void) drawEconomicCalendarBanner:(ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter withEconomicCalendarDataSeletedIndex:(NSNumber *) index
{
    self.indexPosition = viewModel;
    self.painter = painter;
    self.data = viewModel->getData();
    self.economicCalendarDataSeletedIndex = index;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)cgrect
{
    if (![self hasData]){
        return;
    }
    
    // Drawing code.
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    ChartCanvas * ctx = [ChartCanvas new];
    ctx.context = context;
    ctx.bounds = self.bounds;
    
    ViewModelIndexPosition * indexPosition = self.indexPosition;
    
    PainterUserAVG * painter = (PainterUserAVG *) self.painter;
    
    if (self.economicCalendarDataSeletedIndex != nil && YTXConfig::getInstance()->AVG.isDrawEconomicCalendarBanner) {
        [painter drawEconomicCalendarBanner:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withEconomicCalendarDataSeletedIndex:self.economicCalendarDataSeletedIndex];
    }
    
    [painter drawBuyPointsWithinRect:cgrect withContext:ctx withViewModelIndexPosition:indexPosition];
    
    [painter drawLimitPositionInfosWithContext:ctx viewModelIndexPosition:self.indexPosition];
}


@end
